import { reqGetGoodsInfo,reqAddShopCart } from "@/api"
import {getuuid} from '@/utils/uuid_token'

const state = {
    goodInfo: {},
    // 游客临时信息
    uuid_token:getuuid()
}
const actions = {
    // 获取产品信息
    async getGoodInfo({ commit }, skuId) {
        let result = await reqGetGoodsInfo(skuId)
        if (result.code == 200) {
            // console.log(result)
            commit("GETGOODINFO", result.data)
        }
    },
    // 将产品添加到购物车中,传过来的是对象参数,需要解构
    async addShopCart({commit},{skuId,skuNum}){
        let result = await reqAddShopCart(skuId,skuNum)
        // 因为服务器没有返回其余数据,所以不用commit
        if(result.code==200){
            // console.log('成功')
        }else{
            return Promise.reject(new Error('fail'))
        }
    }
}
const mutations = {
    GETGOODINFO(state,goodInfo){
        state.goodInfo = goodInfo
    },
}
const getters = {
    // 简化导航信息
    categoryView(state){
        return state.goodInfo.categoryView || {}
    },
    // 简化产品信息
    skuInfo(state){
        return state.goodInfo.skuInfo || {}
    },
    // 简化售卖信息
    spuSaleAttrList(state){
        return state.goodInfo.spuSaleAttrList || []
    }
}

export default {
    state,
    actions,
    mutations,
    getters,
}